<?php
////////////////////////////////////////////////////////////////////////////////
//	  This file is part of PushLMS.
//
//    PushLMS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    PushLMS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PushLMS.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

class AssessmentsController extends AppController {

	var $scaffold = 'admin';
	var $helpers = array('Form', 'Javascript', 'Ajax');
	var $uses = array('Assessment', 'Course', 'Asession', 'Asessionlog',  'User', 'Assessment', 'Questiongroup', 'Question', 'Answer');

	function beforeFilter() 
	{
		$this->Auth->allow('blank');
		$this->Auth->allow('index_new');
	}

	function index($id = false)
	{
	
		$this->Assessment->unbindModel(
			array('hasMany' => array('Asession'))
		);
		$this->Assessment->unbindModel(
			array('hasMany' => array('Asessionlog'))
		);
		$assessments = $this->Assessment->find('all');
		$this->set('assessments', $assessments);

		

		$cid = 0;
		if ($id == false){
			$cid = -1;
			foreach ($assessments as $assessment_id=>$name){
				$assessment = $this->Assessment->findById($assessment_id);
				break;
			}
		}else{
			$course = $this->Assessment->findByid($id);
			$assessment = $this->Assessment->findById($id);
			$i = 0;
			$cid = $id;
			$assessmentList = array();
			foreach($assessments as $ass)
			{
				if($ass['Course'][0]['id'] == $id)
					$assessmentList[] = $ass;
			}
			
			$this->set('assessments', $assessmentList);
		}
		
		$this->set('cid',$cid);
		$this->set('assessment', $assessment);
		$courses = $this->Course->find('list');
		$this->set('courses',$courses);

		$courseSelect = $this->Course->find('all');
		$this->set('courseSelect',$courseSelect);
	}

	function cheat_sheet($id = -1)
	{
		if (Configure::read('Settings.allow_cheat_sheet') == true){
			set_time_limit(360);
			$this->Assessment->unbindModel(
				array('hasMany' => array('Asession'))
			);
			$this->Assessment->unbindModel(
				array('hasMany' => array('Asessionlog'))
			);
			$this->Assessment->Questiongroup->Question->Answer->unbindModel(
				array('hasMany' => array('Asessionresult'))
			);
			$this->Assessment->Course->unbindModel(array('hasAndBelongsToMany' => array('User','Assessment')));
			$this->Assessment->Questiongroup->unbindModel(array('hasMany' => array('Asessionlog')));
			$this->Assessment->Questiongroup->unbindModel(array('hasAndBelongsToMany' => array('User','Assessment')));
			
			$this->Assessment->Questiongroup->Question->unbindModel(array('hasMany' => array('Asessionlog')));

			//find first assessment
			if($id == -1)
			{
				$assessment = $this->Assessment->find('first');
				$id = $assessment['Assessment']['id'];
			}
			$assessment = $this->Assessment->find('first',
				array
				(
					'conditions' => array('Assessment.id' => $id),
					'recursive' => 3, 
				)
			);
			$this->set('assessment', $assessment);

			$assessment_list = $this->Assessment->find('all');
			$this->set('assessment_list', $assessment_list);
			$this->set('ass_id', $id);

		}else{

			$this->Session->setFlash("This feature is disabled. Please set the allow_cheat_sheet config variable to true to enable this feature.");
			$this->redirect('/users/index');

		}

	}

	public function add(){

		//error_log(print_r($this->data,true));

		$courses = $this->Course->find('list');
		$this->set('courses',$courses);
		if (!empty($this->data)) {

			$this->Assessment->create();
			if ($this->Assessment->save($this->data)){
				
				error_log('new assessment:'.$this->Assessment->id);
				$this->Session->setFlash('Assessment Created');
				$this->set('redirect', true);
			}else{

				error_log('failed to create assessment');
			
			}
		}

		//$this->redirect('/assessments/');

	}

	public function edit($id=null){

		#error_log(print_r($this->data,true));
		if(!empty($this->data)) 
		{
			$this->set('assessment',$this->data);
			error_log(print_r($this->data,true));

			if($this->Assessment->save($this->data)) 
			{
				$this->Session->setFlash('Assessment Updated');
				$this->set('redirect', true);
			}
			
			$courses = $this->Course->find('list');
			$this->set('courses',$courses);
			
	
		}		
		if ($id != null)
		{		
			$assessment = $this->Assessment->findById($id);

			$this->set('assessment',$assessment);
			$this->data = $assessment;
			$courses = $this->Course->find('list');
			$this->set('courses',$courses);
		}

	}

	public function delete($id=null){

		if ($id != null){

			$this->Assessment->delete($id, true);
			$this->set('redirect', true);
		}

		$this->redirect('/assessments/');
	}

	public function duplicate($id=null)
	{
		$assessment = $this->Assessment->findById($id);

		if (!empty($id)) 
		{
			
			$assessment_old = $this->Assessment->findById($id);
			$this->Assessment->unbindModel(
				array('hasMany' => array('Asession'))
			);
			$this->Assessment->unbindModel(
				array('hasMany' => array('Asessionlog'))
			);
			$this->Assessment->Questiongroup->Question->Answer->unbindModel(
				array('hasMany' => array('Asessionresult'))
			);
			$this->Assessment->Course->unbindModel(array('hasAndBelongsToMany' => array('User','Assessment')));
			$this->Assessment->Questiongroup->unbindModel(array('hasMany' => array('Asessionlog')));
			$this->Assessment->Questiongroup->unbindModel(array('hasAndBelongsToMany' => array('User','Assessment')));
			
			$this->Assessment->copy($id);
			//check for a unique name
			$assessment_list = $this->Assessment->find('all');
			$i = 1;
			$searching = true;
			while($searching == true)
			{
				$found = false; //check if an assessment already has the same name.
				$newName = $assessment_old['Assessment']['name'].$i;
				foreach($assessment_list as $assessment)
				{
					if($assessment['Assessment']['name'] == $newName)
					{
						$found = true; //an assessment exists with this name
						break; //break out of foreach and increment i
					}
				}
				
				if($found == false)
				{
					$this->Assessment->saveField('name',$assessment_old['Assessment']['name'].$i);
					$searching = false;
				}
				else
					$i ++;
			}
			$assessment_new = $this->Assessment->findById($this->Assessment->id);

			$qgroup_index = 0;

			foreach ($assessment_old['Questiongroup'] as $questiongroup){
				
				$questiongroup_select = $this->Questiongroup->findById($questiongroup['id']);

				foreach ($questiongroup_select['Question'] as $question){

					$this->Question->copy($question['id']);
					$this->Question->saveField('questiongroup_id',$assessment_new['Questiongroup'][$qgroup_index]['id']);
					#error_log('old qg id: '.$assessment_new['Questiongroup'][$qgroup_index]['id']);
					#error_log('question id:'.$question['id'].' question id new:'.$this->Question->id);
					unset($this->Question->id);

				}
							
				$qgroup_index ++;
			}
		}
		$this->redirect('/assessments/');
	}

	function blank($id=null){
		
		$this->set('assessment_id', $id);

	}
}

?>